package com.qnw.CardGroupManagement.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import android.util.*;

public class LflistConf {
	private Map<String, Map<String, Integer>> map = new HashMap<String, Map<String, Integer>>();
	private volatile static LflistConf lflistConf=null;
	
	private LflistConf(String path) {
		this(new File(path));
	}

	private LflistConf(File file) {
		try {
			Scanner scanner = null;
			String s = null;
			String name = null;
			int num=0;
			Map<String, Integer> m=null;
			try {
				scanner = new Scanner(file);
				while (scanner.hasNextLine()) {
					s = scanner.nextLine();
					if (s.matches("!\\d{4}.*")){
						if(m!=null){
							map.put(name, m);
						}
						name = s.substring(1);
						m=new HashMap<String, Integer>();
					}
					else if (s.matches("#f.*"))
						num=0;
					else if (s.matches("#l.*"))
						num=1;
					else if (s.matches("#s.*"))
						num=2;
					else if (s.matches("\\d{8}.*"))
						m.put(s.substring(0,8), num);
				}
				if(m!=null){
					map.put(name, m);
				}
			} finally {
				if (scanner != null)
					scanner.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static LflistConf getLflistConf(File file) {
		if(lflistConf==null){
			synchronized (LflistConf.class) {
				if(lflistConf==null){
					lflistConf=new LflistConf(file);
				}
			}
		}
		return lflistConf;
	}
	
	public static LflistConf getLflistConf(String path){
		return getLflistConf(new File(path));
	}
	
	public List<String> getNames() {
		List<String> list=new ArrayList<>(map.keySet());
		Collections.sort(list);
		return list;
	}
	
	public int get(String name,String id) {
		return map.get(name).get(id)!=null?map.get(name).get(id):3;
	}
}
